package com.zemb.flxwear.UI.CustomView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.zemb.flxwear.R;

public class CircularSwitch extends View {
    private boolean isOn;
    private Paint paint;
    private int circleColor;
    private int trackColor;
    private int normalColor;
    private float width;
    private float height;
    private OnSwitchListener onSwitchListener;                                                                                                                                                                                                
    public CircularSwitch(Context context) {
        super(context);
        initData();
    }

    public CircularSwitch(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initData();
        initAttr(context,attrs);
    }

    public CircularSwitch(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData();
        initAttr(context,attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
    }
    private void initData(){
        normalColor = Color.GRAY;
        circleColor = Color.GREEN;
        trackColor = Color.GREEN;
        paint =new Paint();
    }

    private void initAttr(Context context  ,@Nullable AttributeSet attrs){
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircularSwitch);
        normalColor = context.getResources().getColor(R.color.gray);
        circleColor = typedArray.getColor(R.styleable.CircularSwitch_circle_color, context.getResources().getColor(R.color.green));
        trackColor = typedArray.getColor(R.styleable.CircularSwitch_track_color,context.getResources().getColor(R.color.green));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(isOn){
            drawOn(canvas);
        }else{
            drawOff(canvas);
        }
    }
    //开关方法
    public void setSwitch(boolean isOn){
        this.isOn = isOn;
        
        
        invalidate();
    }
    //获取开关状态
    public boolean getSwitch(){
 

        return isOn;
    }
    //监听点击事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                setSwitch(!isOn);
                //回调开关状态
                if(onSwitchListener!=null)
                onSwitchListener.onSwitch(isOn);
                break;
        }
        return super.onTouchEvent(event);
    }

    //设置监听
    public void setOnSwitchListener(OnSwitchListener onSwitchListener){
        this.onSwitchListener = onSwitchListener;
    }
    //方法用于绘制开关开启状态
    private void drawOn(Canvas canvas){
        //绘制轨道
        paint.setColor(trackColor);
        paint.setStyle(Paint.Style.FILL);
        //设置圆角
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);
        canvas.drawCircle(height/2,height/2,height/2,paint);
        canvas.drawCircle(width-height/2,height/2,height/2,paint);
        canvas.drawRect(height/2,0,width-height/2,height,paint);
        //绘制圆点
        paint.setColor(circleColor);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(width-height/2,height/2,height/2,paint);
    }

    //方法用于绘制开关关闭状态
    private void drawOff(Canvas canvas){
        paint.setColor(normalColor);
        paint.setStyle(Paint.Style.FILL);
        //设置圆角
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);
        canvas.drawCircle(height/2,height/2,height/2,paint);
        canvas.drawCircle(width-height/2,height/2,height/2,paint);
        canvas.drawRect(height/2,0,width-height/2,height,paint);
        //绘制圆点
        paint.setColor(circleColor);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(height/2,height/2,height/2,paint);
    
    }
    //监听接口回调
    public interface OnSwitchListener{
        void onSwitch(boolean isOn);
    } 
}
